import {
  createRouter,
  createWebHistory // 历史模式
  // createWebHashHistory // hash模式 #
} from 'vue-router'
import { useUserStore } from '@/stores'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
Nprogress.configure({
  showSpinner: false // 配置不显示右侧加载图标
})
const router = createRouter({
  // import.meta.env.BASE_URL 路由的基地址
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: { title: '消息中心' }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: { title: '我的' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: import('@/views/consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: import('@/views/consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: import('@/views/consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: import('@/views/consult/ConsultPay.vue'),
      meta: { title: '支付确认' }
    },
    {
      path: '/room',
      component: import('@/views/room/index.vue'),
      meta: { title: '问诊聊天' },
      // 单个路由的规则 ,进入这个页面前可以做一些判断
      beforeEnter: (to) => {
        if (to.query.payResult === 'false') {
          return '/user/consult'
        }
      }
    },
    {
      path: '/user/consult',
      component: import('@/views/user/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: '三方登录' }
    }
  ]
})
const whiteList = ['/login', '/login/callback']
// 前置导航守卫
router.beforeEach((to) => {
  Nprogress.start()
  const token = useUserStore().user?.token
  // 如果没有登录且访问的是非白名单页面  强制去登录页面
  // vue--router4.x 导航守卫中 return 页面路径 强制去这个页面
  if (!token && !whiteList.includes(to.path)) return '/login'
  // return true  或者 不写 就表示放行
})

// 后置导航守卫
router.afterEach((to) => {
  Nprogress.done()
  if (to.meta.title) {
    document.title =
      '优医问诊-' + to.meta.title + import.meta.env.VITE_APP_TITLE
  } else {
    document.title = '优医问诊-' + ''
  }
  // console.log(to) // 去的页面的详细信息
})
export default router
